﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace eShop.DataAccess
{
    public class BaseDAL
    {
        private static string _defaultConnectionStringName = string.Empty;
        private static string _defaultConnectionString = string.Empty;

        private static string DefaultConnectionStringName
        {
            get
            {
                if (string.IsNullOrEmpty(_defaultConnectionStringName))
                {
                    _defaultConnectionStringName = ConfigurationManager.AppSettings["CurrentDatabase"];
                }
                return _defaultConnectionStringName;
            }
        }
        private static String DefaultConnectionString
        {
            get
            {
                if (String.IsNullOrEmpty(_defaultConnectionString))
                {
                    _defaultConnectionString = ConfigurationManager.ConnectionStrings[DefaultConnectionStringName].ConnectionString;
                }
                return _defaultConnectionString;
            }
        }

        public static Database GetDatabase()
        {
            return new SqlDatabase(DefaultConnectionString);;
        }

        /*
        protected string GetQueryString(string select, string from, string where, string orderBy, string defaultOrderBy)
        {
            where = string.IsNullOrEmpty(where) ? "" : " WHERE " + where;
            orderBy = string.IsNullOrEmpty(orderBy) ? defaultOrderBy : orderBy;
            orderBy = string.IsNullOrEmpty(orderBy) ? "" : " ORDER BY " + orderBy;
            return
            @"
                    SELECT	" + select + " FROM " + from + " " + where + " " + orderBy + @"
            ";
        }

        protected string GetPagedQueryString(string select, string from, string where, string orderBy, string defaultOrderBy, bool allowPaging, int startRowIndex, int maximumRows)
        {
            where = string.IsNullOrEmpty(where) ? "" : " WHERE " + where;
            orderBy = string.IsNullOrEmpty(orderBy) ? defaultOrderBy : orderBy;
            orderBy = string.IsNullOrEmpty(orderBy) ? "" : " ORDER BY " + orderBy;
            if (allowPaging)
            {
                return
                @"
                    WITH DummyTable AS
                    (
                        SELECT	" + select + @", ROW_NUMBER() OVER(" + orderBy + @") AS RowNum
	                    FROM " + from + @" " + where + @"
                    )

                    SELECT * 
                    FROM DummyTable
                    WHERE RowNum BETWEEN " + (startRowIndex + 1) + @" AND " + (startRowIndex + maximumRows) + @";
                ";
            }
            else
            {
                return
                @"
                    SELECT	" + select + " FROM " + from + " " + where + " " + orderBy + @"
                ";
            }
        }

        protected string GetPagedCountQueryString(string from, string where)
        {
            where = string.IsNullOrEmpty(where) ? "" : " WHERE " + where;
            return "SELECT COUNT(1) FROM " + from + " " + where;
        }
        */
    }

}
